﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Effector.Data;
using Effector.ViewModel;

namespace Effector.Dialogs
{
    /// <summary>
    /// Create a new effect file dialog.
    /// </summary>
    /// <remarks>
    /// The dialog does not create the file.It only returns the user selections information
    /// with which to create the file , like the filename and where to create it.Basically only
    /// those two.
    /// </remarks>
    public partial class NewEffectFileDialog : Window
    {
        #region Private Fields

        //the filename we choose the new effect file to have
        private string _selectedEffectFile = string.Empty;

        //the path to open and save the new effect file.
        private string _selectedFilePath = string.Empty;

        #endregion

        #region Public

        /// <summary>
        /// Initializes a new instance of the <see cref="NewEffectFileDialog"/> class.
        /// </summary>
        public NewEffectFileDialog()
        {
            InitializeComponent();

            //holds an observable collection of all the fixed logical drives.
            ExplorerModel em = new ExplorerModel(GetDrives());
            drivesComboBox.DataContext = em as ExplorerModel;
        }

        /// <summary>
        /// Gets or sets the selected effect file.
        /// </summary>
        /// <value>The selected effect file.</value>
        public string SelectedEffectFile
        {
            get { return _selectedEffectFile; }
            set { _selectedEffectFile = value; }
        }

        /// <summary>
        /// Gets or sets the selected file path.
        /// </summary>
        /// <value>The selected file path.</value>
        public string SelectedFilePath
        {
            get { return _selectedFilePath; }
            set { _selectedFilePath = value; }
        }

        /// <summary>
        /// Gets the drives.
        /// </summary>
        /// <remarks>Only the fixed drives are returned.</remarks>
        /// <returns>A list of <see cref="LogicalDriveModel"/> filled with the drives of the system.</returns>
        public LogicalDriveModel[] GetDrives()
        {
            List<LogicalDriveModel> ldm = new List<LogicalDriveModel>();
            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                if (di.DriveType == DriveType.Fixed)
                {
                    LogicalDriveElement lde = new LogicalDriveElement(di);
                    ldm.Add(new LogicalDriveModel(lde, null));
                }
            }

            return ldm.ToArray();
        }

        #endregion


        #region EventHandlers

        /// <summary>
        /// Handles the Clicked event of the OkButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        /// <todo>
        /// check if there are selctions for all that we need.without a path or filename
        /// don't exit or set the defaults.maybe "basic.fx" for filename and the current working 
        /// directory as path.
        /// </todo>
        private void OkButton_Clicked(object sender, RoutedEventArgs e)
        {
            //check if user didn't give a filename and save a default
            if (_selectedEffectFile.Length == 0)
                _selectedEffectFile = "basic.fx";

            //check if the user didn't select a path add assign it to the current working
            if (_selectedFilePath.Length == 0)
                _selectedFilePath = Directory.GetCurrentDirectory();

            // set the dialog result to true.we accept the selections.
            this.DialogResult = true;
        }

        /// <summary>
        /// Called when [combo box selection changed].
        /// </summary>
        /// <param name="sender">The ComboBox for the logical drives.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //create a new explorer model with only one drive in his items list
            //the one we choose from the combobox.set it as the data context in the
            // treeview control.
            LogicalDriveModel ldm = drivesComboBox.SelectedItem as LogicalDriveModel;
            ExplorerModel emtreeview = new ExplorerModel(ldm);
            directoryPathTreeView.DataContext = emtreeview;
         }

        /// <summary>
        /// Called when [tree view selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedPropertyChangedEventArgs&lt;System.Object&gt;"/> instance containing the event data.</param>
        private void OnTreeViewSelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            FileSystemElementModel fsem = directoryPathTreeView.SelectedItem as FileSystemElementModel;
            fsem.GetSelected(out _selectedFilePath);
        }

        /// <summary>
        /// Called when [file name text box text changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> instance containing the event data.</param>
        private void OnFileNameTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            //save the entered text in the textbox to our local variable
            TextBox tb = sender as TextBox;
            _selectedEffectFile = tb.Text;

            //check if no extension is given and add the default
            if (_selectedEffectFile.EndsWith(fileExtensionTextBox.Text) != true)
                _selectedEffectFile += fileExtensionTextBox.Text;
        }

        #endregion
    }
}